package com.itheima.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.sky.dao.OrderMapper;
import com.itheima.sky.dao.ShoppingCartMapper;
import com.itheima.sky.pojo.OrderDetail;
import com.itheima.sky.pojo.Orders;
import com.itheima.sky.pojo.ShoppingCart;
import com.itheima.sky.service.OrderService;
import com.itheima.sky.vo.OrderDetailVO;
import com.itheima.sky.vo.OrderVO;
import com.itheima.sky.vo.PageResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description OrderServiceImpl
 * @Author jiangning
 * @Date 2024-06-28
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    /**
     * 在用户页获取订单列表
     *
     * @param userId
     * @return
     */
    @Override
    public PageResultVO<OrderVO> getOrdersInUserPage(Long userId,Integer startPage,Integer pageSize) {
        PageHelper.startPage(startPage,pageSize);
//        List<Orders> orders = orderMapper.getOrdersByUserId("userId");
        Page<Orders> ordersPage = orderMapper.getOrdersByUserIdPH(userId);
        List<Orders> orders = ordersPage.getResult();
        List<OrderVO> orderVO = orders.stream().map(order->{
            OrderVO newOrderVO = new OrderVO();
            List<OrderDetailVO> orderDetails= orderMapper.getOrderDetailsByOrderIdVO(order.getId());

            Integer sum = 0;
            for (OrderDetailVO orderDetailVO : orderDetails) {
                sum += orderDetailVO.getNumber();
            }

            newOrderVO.setOrderDetails(orderDetails);
            newOrderVO.setSumNum(sum);
            newOrderVO.setOrderTime(order.getOrderTime());
            newOrderVO.setStatus(order.getStatus());
            newOrderVO.setAmount(order.getAmount());

            return newOrderVO;
        }).collect(Collectors.toList());

        return new PageResultVO<>(orderVO,ordersPage.getTotal(),ordersPage.getPageSize(),ordersPage.getPageNum(),ordersPage.getPages());
    }

    /**
     * 获取订单列表（分页）
     *
     * @param userId
     * @param startPage
     * @param pageSize
     * @return
     */

    /**
     * 获取分页的订单信息
     *
     * @param page 当前页码
     * @param pageSize 每页的记录数
     * @param number 订单号（可选，用于过滤）
     * @param beginTime 开始时间（可选，用于过滤）
     * @param endTime 结束时间（可选，用于过滤）
     * @return 包含分页结果的 PageResultVO 对象
     */
    @Override
    public PageResultVO<Orders> getOrdersInPage(int page, int pageSize, String number, String beginTime, String endTime) {
        // 使用 PageHelper 设置分页参数
        PageHelper.startPage(page, pageSize);
        // 从数据库中获取符合条件的订单详情列表
        List<Orders> orders = orderMapper.getOrderDetailPage(number, beginTime, endTime);
        // 使用 PageInfo 对结果进行分页处理
        PageInfo<Orders> pageInfo = new PageInfo<>(orders);
        // 创建并返回包含分页信息的 PageResultVO 对象
        return new PageResultVO<>(
                pageInfo.getList(),    // 当前页的数据列表
                pageInfo.getTotal(),   // 总记录数
                pageInfo.getPageSize(),// 每页的记录数
                pageInfo.getPageNum(), // 当前页码
                pageInfo.getPages()    // 总页数
        );
    }


    //c
    public void submitOrder(Orders order,Long userId) {
        //根据用户id查询当前购物车里的商品
        List<ShoppingCart> shoppingCartList=shoppingCartMapper.selectCartList(userId);
        //创建一个变量存储总金额
        BigDecimal totalAmount = new BigDecimal(0);
        //遍历shoppingCartList里面的数据计算他的总金额并且创建orderDetailList给order_detail表存储数据
        List<OrderDetail> orderDetailList =new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
//            orderDetail.setId(userId); //订单项目的主键
            orderDetail.setName(shoppingCart.getName());  //购物项的菜品的名字
//            orderDetail.setOrderId(userId); //订单项所属的订单的id
            orderDetail.setDishId(shoppingCart.getDishId()); //购物项的菜品的id
            orderDetail.setSetmealId(shoppingCart.getSetmealId()); //购物项的套餐的id
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor()); //口味
            orderDetail.setNumber(shoppingCart.getNumber());  //数量
            orderDetail.setAmount(shoppingCart.getAmount()); //单价
            orderDetail.setImage(shoppingCart.getImage()); //
            //计算每一个购物项的总价
            BigDecimal amount = shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber() + ""));
            totalAmount = totalAmount.add(amount);
            orderDetailList.add(orderDetail);
        }
        //提交到数据库
//        orderMapper.submitOrder(orders);

    }
}
